package cn.jy.careuplusdoctor.mycenter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.adapter.HospitalListAdapter;
import cn.jy.careuplusdoctor.adapter.SectionOfficeListAdapter;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.entity.Hospital;
import cn.jy.careuplusdoctor.entity.Subject;
import cn.jy.careuplusdoctor.http.HttpHelper;
import cn.jy.careuplusdoctor.http.HttpHelper.onHttpListener;
import cn.jy.careuplusdoctor.http.HttpUrls;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.main.RegisterActivity;
import cn.jy.careuplusdoctor.response.HospitalResponse;
import cn.jy.careuplusdoctor.response.SubjectResponse;
import cn.jy.careuplusdoctor.utils.StringUtil;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarLeftAction;

public class HospitalListActivity extends BaseActivity implements TitleBarLeftAction {
	// 邮箱
	@IdInject(id = R.id.list_view)
	private ListView				list_view;
	@IdInject(id = R.id.layout_search)
	private LinearLayout 			layout_search;
	@IdInject(id = R.id.edt_search)
	private EditText				edt_search;
	@IdInject(id = R.id.btn_search)
	private ImageView 				btn_search;
	private HospitalListAdapter		hospitalAdapter;
	private SectionOfficeListAdapter	sectionAdapter;
	private Intent					intent;
	private String					listString;
	private String					hid,activity;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hospital_list);
		intent = getIntent();
		listString = intent.getStringExtra("ListString");
		hid = intent.getStringExtra("hid");
		activity = intent.getStringExtra("activity");
		initTitleBar();
		onInJectView();
		init();
	}

	@Override
	protected void settingTitleBar() {
		super.settingTitleBar();
		if(StringUtil.isNotEmpty(activity)){
			getTitleBar().setLeftText("个人信息");
		}else{
			getTitleBar().setLeftText("注册");
		}
		
		if (listString.equals("医院")) {
			getTitleBar().setTitle("医院列表");
		} else {
			getTitleBar().setTitle("科室列表");
		}
		getTitleBar().getRighTextView().setVisibility(View.GONE);
		getTitleBar().setLeftAction(HospitalListActivity.this);
		getTitleBar().getLeftImageView().setImageResource(R.drawable.title_bar_back);
	}
	
	@Override
	protected void onAfterInJector() {
		super.onAfterInJector();
		if (listString.equals("科室")) {
			layout_search.setVisibility(View.GONE);
		}
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
					case R.id.btn_search:
						((InputMethodManager)edt_search.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
								HospitalListActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
						hospitalList(edt_search.getText().toString().trim());
						break;
				}
			}
		};
		findViewById(R.id.btn_search).setOnClickListener(listener);
		
		edt_search.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId == EditorInfo.IME_ACTION_SEARCH){
					((InputMethodManager)edt_search.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
							HospitalListActivity.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
					hospitalList(edt_search.getText().toString().trim());
				}
				return false;
			}
		});
	}

	public void init() {
		if (listString.equals("医院")) {
			hospitalAdapter = new HospitalListAdapter(HospitalListActivity.this);
			list_view.setAdapter(hospitalAdapter);
			//hospitalList();
		} else {
			sectionAdapter = new SectionOfficeListAdapter(HospitalListActivity.this);
			list_view.setAdapter(sectionAdapter);
			sectionList();
		}
	}

	@Override
	public void onLeftPress(View view) {
		defaultFinish();
	}

	/**
	 * 调用医院列表接口
	 */
	public void hospitalList(String hospitalName) {
		HttpHelper.getInstance().getHospitalList(hospitalName, new onHttpListener<HospitalResponse>() {
			@Override
			public void onResponse(HospitalResponse data, int code) {
				if (data != null && code == HttpUrls.REQUEST_SUCCESS) {
					List<Hospital> doctor = data.getData();
					if (TextUtils.equals("1", data.getResType()) && doctor != null) {
						hospitalAdapter.addAll(doctor);
						hospitalAdapter.notifyDataSetChanged();
						list_view.setOnItemClickListener(new OnItemOnClick(data));
					} else if(TextUtils.equals("0", data.getResType())){
						hospitalAdapter.clearData();
						hospitalAdapter.notifyDataSetChanged();
						shortToast("未搜索到相关医院");
					}else {
						shortToast(data.getMsg());
					}
				} else {
					shortToast("网络故障，获取失败");
				}
			}
		});
	}

	public void sectionList() {
		if (StringUtil.isNotEmpty(hid)) {
			HttpHelper.getInstance().getHospitalSubjectList(hid, new onHttpListener<SubjectResponse>() {
				@Override
				public void onResponse(SubjectResponse data, int code) {
					if (data != null && code == HttpUrls.REQUEST_SUCCESS) {
						List<Subject> doctor = data.getData();
						if (TextUtils.equals("1", data.getResType()) && doctor != null) {
							sectionAdapter.addAll(doctor);
							sectionAdapter.notifyDataSetChanged();
							list_view.setOnItemClickListener(new OnItemOnClick(data));
						} else {
							shortToast(data.getMsg());
						}
					} else {
						shortToast("网络故障，获取失败");
					}
				}
			});
		}
	}
	public class OnItemOnClick implements OnItemClickListener {
		private Object bean;
		private Intent hoIntent;
		private Bundle bundle;
		public OnItemOnClick(Object bean) {
			this.bean = bean;
			hoIntent = new Intent(HospitalListActivity.this,RegisterActivity.class);
			bundle = new Bundle(); 
		}
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			if (bean instanceof HospitalResponse) {
				bundle.putString("name", ((HospitalResponse) bean).getData().get(arg2).getHospitalName());
				bundle.putString("id", ((HospitalResponse) bean).getData().get(arg2).getId());
				hoIntent.putExtras(bundle);
				setResult(1002, hoIntent);
				
			} else if (bean instanceof SubjectResponse) {
				bundle.putString("name", ((SubjectResponse) bean).getData().get(arg2).getSubjectName());
				bundle.putString("id", ((SubjectResponse) bean).getData().get(arg2).getId());
				hoIntent.putExtras(bundle);
				setResult(1003, hoIntent);
			}
			finish();
		}
		
	}
}
